package com.whjz.dcfs.ggts.struts.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.whjz.dcfs.ggts.dao.IDcfsTsclTsjcsjJbxxDAO;
import com.whjz.dcfs.ggts.model.DcfsTsclTsjcsjJbxx;
import com.whjz.dcfs.jzxx.dao.IDcfsDocmentDAO;
import com.whjz.dcfs.jzxx.dao.IDcfsHjjcdJbxxDAO;
import com.whjz.dcfs.jzxx.dao.IDcfsJzSbJbxxDAO;
import com.whjz.dcfs.jzxx.model.DcfsDocment;
import com.whjz.dcfs.jzxx.model.DcfsHjjcdJbxx;
import com.whjz.dcfs.jzxx.model.DcfsJzSbJbxx;
import com.whjz.dcfs.util.BaseAction;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class ExportAction extends BaseAction{
	static final Logger log = LogManager.getLogger(ExportAction.class); 
	private static final long serialVersionUID = 1L;
	private InputStream exportFile;//导出文件的输入流
	private String fileName;//附件名字
	@Autowired
	public IDcfsJzSbJbxxDAO dcfsJzSbJbxxDAO;
	@Autowired
	private IDcfsDocmentDAO dcfsDocumentDAO;
	@Resource(name="freemarkerConfig")
	private Configuration cfg;// freemarker配置
	@Autowired
	private IDcfsHjjcdJbxxDAO dcfsHjjcdJbxxDAO;
	@Autowired
	private IDcfsTsclTsjcsjJbxxDAO dcfsTsclTsjcsjJbxxDAO;
	
	public void ExportToWord() throws Exception{
		getHttpServletRequest().setCharacterEncoding("utf-8");
		String fileName=getHttpServletRequest().getParameter("sbmc")+".doc";
		StringBuffer sb=new StringBuffer(getHttpServletRequest().getParameter("tableInfo"));
		String contentType="application/msword";
		String recommendedName=new String(fileName.getBytes(),"ISO-8859-1");
		getHttpServletResponse().setContentType(contentType);
		getHttpServletResponse().setHeader("Content-Disposition", "attachment;filename="+recommendedName+"\"");
		getHttpServletResponse().resetBuffer();
		ServletOutputStream sos=getHttpServletResponse().getOutputStream();
		sos.write(sb.toString().getBytes());
		sos.flush();
		sos.close();
	}
	public String ExportToWordNew() throws Exception{
		getHttpServletRequest().setCharacterEncoding("utf-8");
		DcfsJzSbJbxx dcfsJzSbJbxx=new DcfsJzSbJbxx();
		dcfsJzSbJbxx.setJzsbbh(getHttpServletRequest().getParameter("jzsbbh"));
		//基站设备信息
		List<DcfsJzSbJbxx> list=dcfsJzSbJbxxDAO.getDcfsJzSbJbxxList_ggts(dcfsJzSbJbxx);
		DcfsDocment dcfsDocument=new DcfsDocment();
		dcfsDocument.setJzsbbh(getHttpServletRequest().getParameter("jzsbbh"));
		dcfsDocument.setDoctype("图片");
		//基站设备图片
		List<DcfsDocment> list2=dcfsDocumentDAO.getDcfsDocmentListByCond(dcfsDocument);
		Template t = cfg.getTemplate("jzsbjbqkWord.ftl");
		Map dataMap = new HashMap();
		dataMap.put("entity", list.get(0)); 
		String[] str;
		List<DcfsDocment> list3=new ArrayList<DcfsDocment>();
		int i=0;
		for (DcfsDocment dcfsDocment : list2) {
			if(i==0){
				dcfsDocment.setDocname("rId5");
			}else{
				dcfsDocment.setDocname("rId"+(i+7));
			}
			i++;
			str=dcfsDocment.getDocurl().split("\\\\");
			dcfsDocment.setDocurl(getImageStr("E:/doc/"+str[str.length-1]));
			list3.add(dcfsDocment);
		}
		dataMap.put("pic",list3);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8");// 在web上要设置流的编码
		t.process(dataMap, osw);
		osw.flush();
		osw.close();
		exportFile= new ByteArrayInputStream(baos.toByteArray());
		fileName = new String((list.get(0).getJzmc()+".doc").getBytes(),"ISO-8859-1");
		baos.close();
		return "word";
	}
	public String ExportToWordNewData() throws Exception{
		getHttpServletRequest().setCharacterEncoding("utf-8");
		//监测数据
		DcfsHjjcdJbxx dcfsHjjcdJbxx=new DcfsHjjcdJbxx();
		dcfsHjjcdJbxx.setJzsbbh(getHttpServletRequest().getParameter("jzsbbh"));
		List<DcfsHjjcdJbxx> list=dcfsHjjcdJbxxDAO.getDcfsHjjcdJbxxByCond_timeNew(dcfsHjjcdJbxx);
		//推算数据
		DcfsTsclTsjcsjJbxx dcfsTsclTsjcsjJbxx=new DcfsTsclTsjcsjJbxx();
		dcfsTsclTsjcsjJbxx.setTsbh(getHttpServletRequest().getParameter("tsbh"));
		List<DcfsTsclTsjcsjJbxx> list2=dcfsTsclTsjcsjJbxxDAO.getDcfsTsclTsjcsjJbxxListByCond(dcfsTsclTsjcsjJbxx);
		Template t = cfg.getTemplate("jzsWord.ftl");
		Map dataMap = new HashMap();
		dataMap.put("list", list);
		dataMap.put("list2", list2); 
		dataMap.put("sbmc", getHttpServletRequest().getParameter("sbmc").toString());
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8");// 在web上要设置流的编码
		t.process(dataMap, osw);
		osw.flush();
		osw.close();
		exportFile= new ByteArrayInputStream(baos.toByteArray());
		fileName = new String((getHttpServletRequest().getParameter("sbmc")+"监测数据信息.doc").getBytes(),"ISO-8859-1");
		baos.close();
		return "word";
	}
	private String getImageStr(String imagePath) {
		String imgFile =imagePath;
		InputStream in = null;
		byte[] data = null;
		try {
			in = new FileInputStream(imgFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (Exception e) {
			log.error("Exception:",e);
		}
		if(in==null){
			return "";
		}else{
		return Base64.encodeBase64String(data);
		}
	}
	public InputStream getExportFile() {
		return exportFile;
	}
	public void setExportFile(InputStream exportFile) {
		this.exportFile = exportFile;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
}
